package leetcode.editor.cn;

import java.util.ArrayList;
import java.util.List;

/**
 * 组合总和 III
 * @author qulq
 * @date 2024-07-07 13:42:36
 */
public class T216_CombinationSumIii{
	 public static void main(String[] args) {
	 	 //测试代码
	 	 Solution solution = new T216_CombinationSumIii().new Solution();
	 }
	 
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {

	List<List<Integer>> ans = new ArrayList<>();
	List<Integer> path = new ArrayList<>();

    public List<List<Integer>> combinationSum3(int k, int n) {
		backtracking(k, n, 1, 0);
		return ans;
    }

	public void backtracking(int k, int n, int start, int sum) {
		if (path.size() == k) {
			if (sum == n) {
				ans.add(new ArrayList<>(path));
			}
			return;
		}

		for (int i = start; i <= 9 - (k - path.size()) + 1 && sum + i <= n; i++) {
			path.add(i);
			backtracking(k, n, i + 1, sum + i);
			path.remove(path.size() - 1);
		}
	}
}
//leetcode submit region end(Prohibit modification and deletion)

}